package com.example.demo.springcycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    private HttpSecurity http;
    AtomicInteger integer = new AtomicInteger();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if("springSecurityFilterChain".equals(beanName)){
            System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
            System.out.println(bean.getClass());
            //处理 org.springframework.security.web.FilterChainProxy
            FilterChainProxy filterChainProxy = (FilterChainProxy)bean;
            final Field field;
            try {
                field = filterChainProxy.getClass().getDeclaredField("filterChains");
                field.setAccessible(true);
                List<SecurityFilterChain> filterChains = (List<SecurityFilterChain>)field.get(bean);
                List<Filter> filters = new ArrayList();
                for (SecurityFilterChain filterChain : filterChains) {
                    filters.addAll(filterChain.getFilters());
                }

               SecurityFilterChain temp = new SecurityFilterChain() {
                    @Override
                    public boolean matches(HttpServletRequest request) {
                        return true;
                    }
                    @Override
                    public List<Filter> getFilters() {
                        return filters;
                    }
                };
                filterChains.clear();
                filterChains.add(temp);

                field.set(bean,filterChains);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if("webSecurityConfig".equals(beanName)){
           // System.out.println("=========="+integer.getAndIncrement()+"webSecurityConfig");
            //WebSecurityConfigurerAdapter webSecurity = (WebSecurityConfigurerAdapter)bean;
        }else if("webSecurityConfig2".equals(beanName)){
            //System.out.println("=========="+integer.getAndIncrement()+"webSecurityConfig2");
            //WebSecurityConfigurerAdapter webSecurity = (WebSecurityConfigurerAdapter)bean;
        }else if("springSecurityFilterChain".equals(beanName)){
            System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
            //System.out.println("=========="+integer.getAndIncrement()+"springSecurityFilterChain");
            //System.out.println(bean.getClass());
            //处理 org.springframework.security.web.FilterChainProxy
            FilterChainProxy filterChainProxy = (FilterChainProxy)bean;
            final List<SecurityFilterChain> filterChains = filterChainProxy.getFilterChains();
            System.out.println("SecurityFilterChain.size["+filterChains.size()+"]，filter.size["+filterChains.get(0).getFilters().size()+"]");
        }

        return bean;
    }
}
